package com.imooc.miaosha.controller.FeController;

import com.alibaba.fastjson.TypeReference;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.imooc.miaosha.common.Const;
import com.imooc.miaosha.dto.OrderDTO;
import com.imooc.miaosha.dto.OrderDetailDTO;
import com.imooc.miaosha.dto.UserDTO;
import com.imooc.miaosha.pojo.MiaoshaGoods;
import com.imooc.miaosha.service.OrderService;
import com.imooc.miaosha.util.CookieUtil;
import com.imooc.miaosha.util.JedisUtil;
import com.imooc.miaosha.util.JsonData;
import com.imooc.miaosha.util.JsonUtil;
import com.imooc.miaosha.vo.OrderShippingVO;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.el.lang.ELArithmetic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    DefaultKaptcha defaultKaptcha;
    @Autowired
    OrderService orderService;
    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;

    @RequestMapping("/order_list")
    public String list(Model model, HttpServletRequest request) {
        String loginToken = CookieUtil.readCookie(request);
        String userJson = JedisUtil.get(Const.LOGIN_TOKEN_PREFIX, loginToken);
        UserDTO userDTO = JsonUtil.String2Obj(userJson, new TypeReference<UserDTO>() {
        });
        if (userDTO == null) {
            return "未登录";
        }
        List<OrderShippingVO> orderShippingVOList = orderService.list(userDTO.getId());
        model.addAttribute("orderDTOList", orderShippingVOList);
        return "order_list";

    }

    @RequestMapping("/order_list_mini")
    @ResponseBody
    public JsonData list(String token) {
        String userJson = JedisUtil.get(Const.LOGIN_TOKEN_PREFIX, token);
        UserDTO userDTO = JsonUtil.String2Obj(userJson, new TypeReference<UserDTO>() {
        });
        if (userDTO == null) {
            return JsonData.fail("未登录");
        }
        List<OrderShippingVO> orderShippingVOList = orderService.list(userDTO.getId());
        return JsonData.success(orderShippingVOList);

    }

    @RequestMapping(value = "/detail/{id}", produces = "text/html")
    @ResponseBody
    public String detail(@PathVariable("id") Long orderId, Model model, HttpServletRequest request, HttpServletResponse response) {
        String loginToken = CookieUtil.readCookie(request);
        String userJson = JedisUtil.get(Const.LOGIN_TOKEN_PREFIX, loginToken);
        UserDTO userDTO = JsonUtil.String2Obj(userJson, new TypeReference<UserDTO>() {
        });
        if (userDTO == null) {
            return "用户登录会话过期,请重新登录";
        }
        String html = JedisUtil.get("ORDER_DETAIL", orderId.toString());
        if (html != null) {
            return html;
        } else {
            OrderDetailDTO orderDetailDTO = orderService.detail(orderId, userDTO.getId());
            model.addAttribute("orderDetail", orderDetailDTO);
            WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
            html = thymeleafViewResolver.getTemplateEngine().process("order_detail", webContext);
            JedisUtil.setEx("orderDetail", orderId.toString(), html, 60);
            return html;
        }

    }


}
